/**
 * 基本药物弹窗模块，点击图表首页后初始化弹窗数据
 * 
 * @Date: 2016-1-25
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */

define(["jquery","sysmanMain","valueUtil","panelUtil","btnServ","datagridServ","commonServ","formServ"],
		function($,sysmanMain,valueUtil,panelUtil,btnServ,datagridServ,commonServ,formServ) {

		var rootid = 'panel-overlay-prescribe-pop';
		
		var window_id = 'overlay-prescribe-pop-win';
		
		var module_code ='overlayPrescribeInfo';
		var module_code_head = 'overlayPrescribe';
		var queryParam ={};
		
		
		//类型定义
		var defaultLayer = {
			layer		: 'dept',	//层定义
			nextLayer	: 'doct',
			grid		: 'dg-dept-list',		//列表对象
			condition	: null,
			dept_code	: null,		//医生
			dept_name 	: null ,//患者		
			mzhm		: null //当前门诊患者记录id
		}		
		
		
		//当前查看信息
		var currentLayer = {
			dept_code	: null, //当前科室id			
			doct_code	: null,	//当前医生
			grid		: null,
			mzhm		: null, //当前门诊患者记录id			
			layer		: null,		//当前层级
			condition	: null
		}		
		
		
		//格式化函数
		var formatValue=function(value,row,index){
			if (value > 0){
				return   (value*100).toFixed(2) +'%';							
			}else{
				return '0.00%';
			}
		}
		
		//基本字段定义
		var base_config = {			
			//合计字段
			base_all_columns	:[
				{field:'outpatient_num',title:'门诊人次',halign:'center',width:30},
				{field:'maybe_price_over',title:'处方价格超标(疑似)',width:30,align:'center',halign:'center'},
				{field:'drug_price_overlay',title:'处方价格超标',halign:'center',width:30},				
				{field:'maybe_date_over',title:'用药周期超标(疑似)',width:30,align:'center',halign:'center'},
				{field:'drug_date_overlay',title:'用药周期超标',width:30,align:'center',halign:'center'},				
				{field:'maybe_type_over',title:'用药种类超标(疑似)',width:30,align:'center',halign:'center'},
				{field:'drug_type_overlay',title:'用药种类超标',width:30,align:'center',halign:'center'}	
			]
		}
		
		var columns_config = {			
			
			deptColumns	: [
				[{field:'dept_name',title:'科室名称',width:50,align:'center',halign:'center'}]
					.concat(base_config.base_all_columns)]
			
		}
		
		var url = '/overlayPrescribe/query/getOverlayPrescribeAbnormal.action';
		
		var patientUrl = '/overlayPrescribe/query/getOutpatientListDomain.action';
		var _buttonConfig;
		
		//调用参数缓存
		var call_options = {
		
			rootid		: null,
			condition	: null,
			cellClick	: null
		}
		var public_method ={
			
			/**
			 *  @param _options.rootid	根节点
			 *  
			 *  @param _options.condition	外部查询条件
			 *  
			 *  @param _options.cellClick	字段点击事件响应函数
			 */
			init:function(_options){
				
				
				//判断是否外部传入
				if(_options){ 
					call_options = _options;
					
					if (_options.rootid){					
						rootid = _options.rootid;
					}
					if(_options.condition){
						queryParam = _options.condition;	
					}
				}
				//清除缓存
				currentLayer = $.extend({},defaultLayer);
				
				public_method.recreateGrid();	
				
			}
			
			,
			/**
			 * 重建grid
			 * @param _options.layer	重建grid类型
			 */
			recreateGrid:function(_options){				
				
				
				//清理数据
				currentLayer.dept_code = null;
				currentLayer.doct_code = null;
				currentLayer.mzhm = null;
				
				var _grid = $('#'+rootid + ' .' + currentLayer.grid);
				var _param = {
					url			: '/'+sysmanMain.getRootPath()+ url,
					rownumbers  : true,
					animate		: true,						
					striped		: true,
					border		: true,					
					idField		: 'id',						
					fitColumns	: true,
					
					onClickCell : private_method.editCell,
					columns		: columns_config.deptColumns
				}
				
				var	_queryParams = $.extend({},queryParam,{									
						layer		: currentLayer.layer
					});
							 
				
				//获取查询参数
				_param.queryParams={condition:_queryParams}
				_grid.datagrid(_param);		
				
			}
			
		}
		
		var private_method ={
			
			
			editCell:function(index,field,value){
				
				if(window.console)
					console.info('overlayDept editCell fired ...')			
				
				var breadParam = {}
				var _g = $('#'+rootid + ' .' + currentLayer.grid);
				var rows = _g.datagrid('getRows')
				if(rows.length>=index){
					var row = rows[index];
					if(row){
						var checked = false;
						
						if(!checked && row.dept_code){
							//当前在科室层
							currentLayer.dept_code = row.dept_code;							
							
							breadParam.name = row.dept_name;
							//清除数据
							currentLayer.doct_code = null;
							currentLayer.mzhm = null;							
						}
					}
					
					var _filter = {}
					switch(field){
						case 'maybe_date_over':
							
						case 'drug_date_overlay':
							_filter = {maybe_date_over:true}
							break;
						case 'maybe_price_over':
							
						case 'drug_price_overlay':
							_filter = {maybe_price_over:true}
							break;
						case 'maybe_type_over':
							
						case 'drug_type_overlay':
							_filter = {maybe_type_over:true}
							break;
						default:
					}
					
					//需要保存的状态值
					var statParam = {
						dept_code	: row.dept_code,
						dept_name	: row.dept_name
					}
					if(call_options.cellClick){
						call_options.cellClick({
							row			: row,
							field		: field,
							value		: value,
							state		: $.extend({},statParam,_filter),
							layer		: currentLayer.layer,
							nextLayer	: defaultLayer.nextLayer
						})
					}
				}
				
			}
			
		}
			
		
		return public_method;
			
})